import api from "../common/api";

const app = getApp()

const moduleConfig = {
  memberCenter: '', // 会员中心状态  0: 关闭  1：开启
}

const updateModuleConfig = (info) => {
  if (!info) return;
  Object.assign(moduleConfig, info)
}

const safetyModuleConfig = (info) => {
  if (!info) return;
  let keys = Object.keys(info);
  keys.forEach((key, index) => {
    if (info[key]) {
      moduleConfig[key] = info[key];
    }
  });
}

// -------------------------------会员/支付中心模块-------------------------------------
const getMemberCenter = () => {
  return new Promise((resolve, reject) => {
    if (moduleConfig.memberCenter) {
      resolve(moduleConfig.memberCenter)
      return
    }
    app.globalData.loginPromise.then(() => {
      app.globalData.systemPromise.then(({
        deviceInfo = {}
      }) => {
        api.getReferenceDetailInfoByPhone({
          scene: app.globalData.scene || '',
          deviceInfo: deviceInfo.system || '',
        }, (res) => {
          if (res && res.data && res.data.isSuccess) {
            updateModuleConfig({
              memberCenter: res.data.content
            })
            resolve(res.data.content)
          } else {
            reject()
          }
        })
      })
    })

  })
}

const updateMemberCenter = () => {
  return new Promise((resolve, reject) => {
    app.globalData.loginPromise.then(() => {
      app.globalData.systemPromise.then(({
        deviceInfo = {}
      }) => {
        api.getReferenceDetailInfoByPhone({
          scene: app.globalData.scene || '',
          deviceInfo: deviceInfo.system || '',
        }, (res) => {
          if (res && res.data && res.data.isSuccess) {
            updateModuleConfig({
              memberCenter: res.data.content
            })
            resolve(res.data.content)
          } else {
            reject()
          }
        })
      })
    })
  })
}

// -----------------------------------------------------------------
export {
  getMemberCenter,
  updateMemberCenter
}